How To Build zlib for ARM


Intro


In this How To we are going to build zlib 1.2.8 with our GCC 4.8.2 cross-compiler. Be sure to start this build in a new terminal/tab to avoid any pollution from previous builds.

Tar Balls


Here is a list of source packages that we'll need for the build. You can either download them now or wait 'til later in the How To.
  • zlib-1.2.8.tar.gz

Create a Workspace


I recommend creating a workspace under your /home/<your user>/ directory that is dedicated to this build. So let's fire up your terminal and run the following:
$ export ZLIB_SRC=~/workbench/zlib/src
$ mkdir -pv ~/workbench/zlib
$ mkdir $ZLIB_SRC
$ cd $ZLIB_SRC

Gather the Sources


Now that we have a workspace created and we are currently in the src directory we can begin bringing down the sources and extracting them.

zlib

$ wget zlib.net/zlib-1.2.8.tar.gz
$ tar -pxzf zlib-1.2.8.tar.gz

Build Environment


To make things a little smoother let's setup some environment variables:
$ export INSTALLDIR=~/workbench/gcc-4.8.2/arm
$ export PATH=$INSTALLDIR/bin:$PATH
$ export TARGETMACH=arm-none-linux-gnueabi
$ export BUILDMACH=i686-pc-linux-gnu
$ export CROSS=arm-none-linux-gnueabi
$ export CC=${CROSS}-gcc
$ export LD=${CROSS}-ld
$ export AS=${CROSS}-as
NOTE: Depending on whether you are using a cross-compiler built from my other wikis you might need to change INSTALLDIR to point to your cross-compiler.

Build zlib


$ ./configure --prefix=/home/<your user>/workbench/zlib/final
$ make
$ make install

Output


cd into the final directory and output its contents:
$ cd ../final/
$ ls
You should have the following directories:
  • include
  • lib
  • share
  • var

Lastly, move the contents of these directories, or the directories themselves if they do not already exist, to your custom Linux file system or dev board. For zlib, the files should go under /usr except for var, it goes under root, /.

Usage


To use zlib all you need to do is set the following in a new terminal/tab:
$ export PKG_CONFIG_PATH=/home/<your user>/workbench/zlib/final/lib/pkgconfig
I have had the best luck cross-compiling source code that has dependencies where pkgconfig is available. zlib is one of them. If you have pkgconfig available, use it. You'll normally find it under /lib or ../lib.